﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace h1
{
    public class FormAdapter
    {
        private float originalWidth;
        private float originalHeight;

        public FormAdapter(Form form)
        {
            originalWidth = form.Width;
            originalHeight = form.Height;

            form.Load += Form_Load;
            form.Resize += Form_Resize;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            StoreControlProperties((Control)sender);
        }

        private void Form_Resize(object sender, EventArgs e)
        {
            ResizeControls((Control)sender);
        }

        private void StoreControlProperties(Control control)
        {
            foreach (Control con in control.Controls)
            {
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;

                if (con.Controls.Count > 0)
                    StoreControlProperties(con);
            }
        }

        private void ResizeControls(Control control)
        {
            float scaleX = control.Width / originalWidth;
            float scaleY = control.Height / originalHeight;

            foreach (Control con in control.Controls)
            {
                if (con.Tag != null)
                {
                    string[] properties = con.Tag.ToString().Split(':');

                    float width = Convert.ToSingle(properties[0]) * scaleX;
                    float height = Convert.ToSingle(properties[1]) * scaleY;
                    float left = Convert.ToSingle(properties[2]) * scaleX;
                    float top = Convert.ToSingle(properties[3]) * scaleY;
                    float fontSize = Convert.ToSingle(properties[4]) * scaleY;

                    con.Width = (int)width;
                    con.Height = (int)height;
                    con.Left = (int)left;
                    con.Top = (int)top;

                    if (fontSize > 0)
                        con.Font = new Font(con.Font.Name, fontSize, con.Font.Style, con.Font.Unit);

                    if (con.Controls.Count > 0)
                        ResizeControls(con);
                }
            }
        }
    }
}